//
//  RJOutCityView.m
//  AirQuality
//
//  Created by Po on 15/7/3.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJOutCityView.h"

@interface RJOutCityView ()

@property (retain, nonatomic)NSArray * cityArray;                       //城市数据
@property (retain, nonatomic)NSMutableArray * currentDataArray;         //当前数据

@property (retain, nonatomic)UIImageView * pointImage;                  //地点表示图标
@property (retain, nonatomic)UIImageView * arrowImage;                  //选择箭头
@property (retain, nonatomic)UILabel * cityTitleLabel;                  //选中城市
@property (retain, nonatomic)UIView * allCityView;                      //所有城市View

@property (assign, nonatomic)BOOL isShow;                               //是否正在展示
@property (assign, nonatomic)BOOL isMoving;                             //是否正在动画
@property (assign, nonatomic)CGRect trueRect;
@property (assign, nonatomic)BOOL isShowTitle;                             //是否正在动画

- (void)initData;
- (void)initInterface;
@end

@implementation RJOutCityView

- (instancetype)initWithFrame:(CGRect)frame isShowTitle:(BOOL)title
{
    self = [super initWithFrame:frame];
    if (self) {
        _trueRect = frame;
        _isShowTitle = title;
        [self initInterface];
    }
    return self;
}
#pragma mark - user-defined initialization function
- (void)initData
{
    _isShow = NO;
    _currentRow = 0;
}

- (void)initInterface
{
    //self init
//    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"OutCiytsBack"]];
    [self addSubview:[self getCityBar]];
    [self.layer setCornerRadius:5];
    self.layer.masksToBounds = YES;
}
/**表头*/
- (UIButton *)getCityBar
{
    if (!_cityBar) {
        _cityBar = [[UIButton alloc] initWithFrame:self.bounds];
        [_cityBar setBackgroundColor:[UIColor clearColor]];
        //左侧图标
        
//        if (![RJUserData defaultRJUserData].isProvince) {
//            return _cityBar;
//        }
//        [_cityBar setTitle:[RJUserData defaultRJUserData].currentCity forState:UIControlStateNormal];
        [_cityBar.titleLabel setFont:[UIFont systemFontOfSize:15]];
        if (!_isShowTitle) {
            [_cityBar setTitle:@"" forState:UIControlStateNormal];
            _image = [[UIImageView alloc] initWithFrame:CGRectMake(50, 10, 13, 10)];
            _image.image = [UIImage imageNamed:@"箭头"];
            if (![RJUserData defaultRJUserData].isProvince) {
                _cityBar.userInteractionEnabled = NO;
            }else{
                [_cityBar addSubview:_image];
            }
        }
        if (_isShowTitle) {
            [_cityBar setTitle:@"城市" forState:UIControlStateNormal];
            _cityBar.titleLabel.font = [UIFont systemFontOfSize:16];
            [_cityBar setImageEdgeInsets:UIEdgeInsetsMake(15, 15, 15, 45)];
            _cityBar.layer.borderWidth = 2;
            _cityBar.layer.borderColor = GetRGBColor(27, 170, 169, 1).CGColor;
            _cityBar.layer.cornerRadius = 5;
        }
        [_cityBar setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [_cityBar addTarget:self action:@selector(pressCityButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cityBar;
}

- (UIView *)getAllCityView:(CGRect)frame
{
    if (!_allCityView) {
        _allCityView = [[UIView alloc] initWithFrame:frame];
        [_allCityView setBackgroundColor:[UIColor clearColor]];
        
        for (NSUInteger i = 0; i < _cityArray.count; i ++) {
            UIButton * button = [self createButton:CGRectMake(80 * (i % 3), 5 + 40 * (i / 3), 80, 40) title:[_cityArray[i] allValues][0]];
            [button setTag:500+ i];
            [_allCityView addSubview:button];
            
            //竖线
            NSUInteger count = i % 3;
            if(count != 2) {
                [_allCityView addSubview:[self createLine:CGRectMake(80 * (count + 1), 5 + 40 * (i / 3) + 12.5, 1, 15)]];
            }
        }
    }
    return _allCityView;
}

- (UIButton *)createButton:(CGRect)frame title:(NSString *)title
{
    UIButton * button = [[UIButton alloc] initWithFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pressCityButton:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (UIView *)createLine:(CGRect)frame
{
    UIView * view = [[UIView alloc] initWithFrame:frame];
    [view setBackgroundColor:[UIColor whiteColor]];
    return view;
}
#pragma mark - event
- (void)pressCityButton
{
    _isShow = !_isShow;
    [RJUserData defaultRJUserData].paoPaoName = @"";
    [self setViewFrame:_isShow];
    if (_delegate && [_delegate respondsToSelector:@selector(didPressCityBar:)]) {
        [_delegate didPressCityBar:_isShow];
    }
  
}

#pragma mark - function
- (NSArray *)getCitys
{
    return _cityArray;
}

- (void)setData:(NSArray *)data
{
    _cityArray = data;
}

- (void)setViewFrame:(BOOL)isShow
{
    _isShow = isShow;
    //获取动作位置
    CGRect rect = self.frame;
    CGFloat width = 180;
    CGFloat height = 260;
    if (_isShow) {
        [[self getCityBar] setHidden:YES];
        self.backgroundColor = SysMainColor;
    }
    
    if (_isShow) {
        if (!_isShowTitle) {
          rect.origin.x -= width-80;
        }
        if (_isShowTitle) {
           rect.origin.x -= width; 
        }
        rect.size.width += width;
        rect.size.height += height;
        [self addSubview:[self getAllCityView:CGRectMake(0, 0, rect.size.width, rect.size.height)]];
    } else {
        rect = _trueRect;
    }
    //背景动画
    [UIView animateWithDuration:0.4 animations:^{
        if (!_isShow) {
            [_allCityView setAlpha:0.0];
        } else {
            [_allCityView setAlpha:1.0];
        }
    } completion:^(BOOL finished) {
        if (finished && !_isShow) {
            [[self getCityBar] setHidden:NO];
        }
    }];
    
    [UIView animateWithDuration:0.5f animations:^{
        [self setFrame:rect];
    } completion:^(BOOL finished) {
        if (finished && !isShow) {
            self.backgroundColor = [UIColor clearColor];
        }
    }];
}

- (void)simulationPressCityWithRow:(NSUInteger)row
{
    [self pressCityWithRow:row];
}

- (void)pressCityWithRow:(NSUInteger)row
{
    _currentRow = row;
    if (_delegate) {
        [_delegate didSelectedRow:row data:_cityArray[row]];
    }
//    NSString * text = [[_cityArray[row] allValues] lastObject];
//    [[self getCityBar] setTitle:text forState:UIControlStateNormal];
    [self setViewFrame:NO];
    if (_delegate) {
        [_delegate didPressCityBar:NO];
    }
}

- (void)pressCityButton:(UIButton *)button
{
    
    [self pressCityWithRow:button.tag - 500];
}

@end
